package com.kmproject.haifu.mvp.presenter;

import android.app.Application;

import com.jess.arms.integration.AppManager;
import com.jess.arms.di.scope.ActivityScope;
import com.jess.arms.mvp.BasePresenter;
import com.jess.arms.http.imageloader.ImageLoader;

import io.reactivex.Observable;
import me.jessyan.rxerrorhandler.core.RxErrorHandler;
import me.jessyan.rxerrorhandler.handler.ErrorHandleSubscriber;

import javax.inject.Inject;

import com.kmproject.haifu.app.utils.AppUtil;
import com.kmproject.haifu.app.utils.RxUtils;
import com.kmproject.haifu.mvp.contract.SecondHandImportToStoreContract;
import com.kmproject.haifu.mvp.model.api.Api;
import com.kmproject.haifu.mvp.model.api.service.SecondhandService;
import com.kmproject.haifu.mvp.model.entity.ResultBean;
import com.kmproject.haifu.mvp.model.entity.beans.SecondHandInfoBean;
import com.kmproject.haifu.mvp.model.entity.resbean.ObtainParmartBean;
import com.kmproject.haifu.mvp.model.entity.resbean.SecondHandIds;

import java.util.ArrayList;
import java.util.List;
import java.util.Map;


/**
 * ================================================
 * Description:
 * <p>
 * Created by MVPArmsTemplate on 09/04/2019 10:59
 * <a href="mailto:jess.yan.effort@gmail.com">Contact me</a>
 * <a href="https://github.com/JessYanCoding">Follow me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms">Star me</a>
 * <a href="https://github.com/JessYanCoding/MVPArms/wiki">See me</a>
 * <a href="https://github.com/JessYanCoding/MVPArmsTemplate">模版请保持更新</a>
 * ================================================
 */
@ActivityScope
public class SecondHandImportToStorePresenter extends BasePresenter<SecondHandImportToStoreContract.Model, SecondHandImportToStoreContract.View> {
    @Inject
    RxErrorHandler mErrorHandler;
    @Inject
    Application mApplication;
    @Inject
    ImageLoader mImageLoader;
    @Inject
    AppManager mAppManager;

    @Inject
    public SecondHandImportToStorePresenter(SecondHandImportToStoreContract.Model model, SecondHandImportToStoreContract.View rootView) {
        super(model, rootView);
    }

    /**
     * 得到选中的id
     * @param secondHandInfoBeans
     * @return
     */
    public List<String> getSelectIndex(List<SecondHandInfoBean> secondHandInfoBeans)
    {
        List<String> ids=new ArrayList<>();
        if (secondHandInfoBeans==null||secondHandInfoBeans.isEmpty())
        {

            return ids;
        }
           for (SecondHandInfoBean secondHandInfoBean:secondHandInfoBeans)
           {
               if (secondHandInfoBean.isHaveSelect())
               {
                   ids.add(secondHandInfoBean.getSecondHandId());
               }
           }
           return ids;

    }

    /**
     * 导入到店铺
     * @param secondhandId
     * @param ids
     */
    public void  importSecondhanToStore(String secondhandId,List<String> ids) {

        SecondHandIds secondHandIds=new SecondHandIds();
        secondHandIds.setSecondHandIds(ids);
        mModel.importSecondhanToStore(Api.SECONDHAND_IMPORT_TO_STORE+secondhandId,AppUtil.entityToMap(secondHandIds)).compose(RxUtils.applySchedulers(mRootView))
                .subscribe(new ErrorHandleSubscriber<ResultBean>(mErrorHandler) {
                    @Override
                    public void onNext(ResultBean resultBean) {
                     if (AppUtil.isSuccess(resultBean))
                     {

                        mRootView.importSecondhanToStoreSuccess();
                     }else {
                         mRootView.showMessage(resultBean.getMessage());
                     }
                    }
                });
    }

    /**
     * 查询导入列表
     * @param obtainParmartBean
     */
    public void  querySecondHandToImport(ObtainParmartBean obtainParmartBean) {
      mModel.querySecondHandToImport(AppUtil.entityToMap(obtainParmartBean)).compose(RxUtils.applySchedulers(mRootView))
      .subscribe(new ErrorHandleSubscriber<ResultBean<List<SecondHandInfoBean>>>(mErrorHandler) {
          @Override
          public void onNext(ResultBean<List<SecondHandInfoBean>> listResultBean) {
           if (AppUtil.isSuccess(listResultBean))
           {
               mRootView.querySecondHandToImportSuccess(Integer.parseInt(listResultBean.getExtra().getPage().getCurrent()),Integer.parseInt(listResultBean.getExtra().getPage().getPages()),listResultBean.getData());

           }else {
               mRootView.showMessage(listResultBean.getMessage());
           }
          }
      });
    }
    @Override
    public void onDestroy() {
        super.onDestroy();
        this.mErrorHandler = null;
        this.mAppManager = null;
        this.mImageLoader = null;
        this.mApplication = null;
    }
}
